package com.jy.study.leetcode;


/**
 * @Classname Solution
 * @Description 感谢努力的自己
 * @Date 2019/10/12 13:18
 * @Created by  jiaoyong
 */
public class Solution {

    //给定一个排序数组，你需要在原地删除重复出现的元素，使得每个元素只出现一次，
    //返回移除后数组的新长度。不要使用额外的数组空间，
    //你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
    public static void main(String[] args) {

        int[] ints = {0, 1, 1, 1,1,1,1,2,2,2,3,3,4,5};
        int i = removeDuplicates(ints);
        System.out.println("i = " + i);


    }

    private static int removeDuplicates(int[] nums) {

        int a = 0;
        for (int i = 1; i < nums.length; i++) {
            int different = getDifferent(i, nums);
            if (different != 0) {
                nums[i] = nums[different];
            } else {
                a = i;
                while (i < nums.length - 1) {
                    i++;
                    int dif = getDifferent(i, nums);
                    if (dif != 0) {
                        nums[a] = nums[dif];
                        i = a;
                        break;
                    }
                }
            }
        }

        int b = 0;
       A: for (int i = 1; i < nums.length; i++) {
            b = i;
            if (nums[b] != nums[i - 1]) {
                for (int j = b - 1; j > 0; j--) {
                    if (nums[b] == nums[j - 1]) {
                        a = b;
                        break A;
                    }
                }
                continue;
            } else {
                a = b;
                break A;
            }

        }
        return a;
    }

    //获取不重复元素的索引
    private static int getDifferent(int index, int[] nums) {

        int a = index;
        while (index >= 1 && index < nums.length) {
            if (nums[a - 1] != nums[index]) {
                for (int i = a - 1; i > 0; i--) {
                    if (nums[i - 1] == nums[index]) {
                        return 0;
                    }
                }
                return index;
            }
            index++;
        }
        return index - 1;
    }
}
